<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_Chart"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      sellChart: null,
      sellerData: [],
      totalPage: '',
      curetPage: 1,
      timerId: '',
      startValue: 0,
      endValue: 9,
      // 圆环坐标
      centerArr: [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%'],
      ],
      // 圆环渐变色
      colorArr: [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF'],
      ],
    }
  },
  computed: {
    ...mapState(['theme']),
  },
  watch: {
    theme() {
      this.sellChart.dispose() // 销毁当前的图表
      this.initSellerData() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.upDataCharts() // 更新图表的展示
    },
  },
  created() {
    // window.removeEventListener(this.screenAdapter)
    this.$socket.registerCallBack('stockData', this.getData)
  },

  mounted() {
    this.initSellerData()
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: '',
    })

    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    // this.getSellerData()
  },
  destroyed() {
    clearInterval(this.timerId)
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('stockData')
  },
  methods: {
    initSellerData() {
      console.log(this.theme)
      this.sellChart = this.$chart.init(this.$refs.stock_Chart, this.theme)
      // const colorArr = [
      //   ['#0BA82C', '#4FF778'],
      //   ['#2E72BF', '#23E5E5'],
      //   ['#5052EE', '#AB6EE5'],
      // ]
      let options = {
        title: {
          text: '▎库存和销量分析',
          left: 20,
          top: 20,
        },
      }

      this.sellChart.setOption(options)
    },
    upDataCharts() {
      const titleFontSize = (this.$refs.stock_Chart.offsetWidth / 100) * 3.6
      const innerRadius = titleFontSize * 2.8
      const outerRadius = innerRadius * 1.2
      let start = (this.curetPage - 1) * 5
      let end = (this.totalPage - 1) * 5
      let showData = this.sellerData.splice(start, end)
      let seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          radius: [outerRadius, innerRadius],
          label: {
            fontSize: titleFontSize / 1.2,
          },
          center: this.centerArr[index],
          hoverAnimation: false,
          // 隐藏指示线条
          labelLine: {
            show: false,
          },
          label: {
            position: 'center',
            color: this.colorArr[index][0],
          },
          data: [
            // 销量
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                // 创建线性渐变的颜色 从下往上
                color: new this.$chart.graphic.LinearGradient(0, 1, 0, 0, [
                  // 0%
                  { offset: 0, color: this.colorArr[index][0] },
                  // 100%
                  { offset: 1, color: this.colorArr[index][1] },
                ]),
              },
              // 内部的提示框 c数值 d百分比
              tooltip: {
                formatter: `${item.name} <br/>销量：{c} <br/>占比：{d}%`,
              },
            },
            // 库存
            {
              value: item.stock,
              itemStyle: {
                color: '#bbb',
              },
              // 内部的提示框
              tooltip: {
                formatter: `${item.name} <br/>库存：{c} <br>占比：{d}%`,
              },
            },
          ],
        }
      })
      const dataOption = {
        tooltip: {
          // 这里为item 可以为内部的数据开启 单独的 tooltip
          trigger: 'item',
        },
        series: seriesArr,
      }

      this.sellChart.setOption(dataOption)
      // this.sellChart.setOption(dataOption)
      this.sellChart.on('mouseover', () => {
        this.timerId && clearInterval(this.timerId)
      })
      // 鼠标移出图表的时候 定时器开启
      this.sellChart.on('mouseout', () => {
        this.setIntervalData()
      })
    },
    async getData(ret) {
      // let res = await this.$axios.get('stock.json')
      // this.sellerData = res.data
      this.sellerData = JSON.parse(ret)
      this.totalPage = Math.ceil(this.sellerData.length / 5)
      this.upDataCharts()
      this.setIntervalData()
    },
    setIntervalData() {
      this.timerId = setInterval(() => {
        this.curetPage++
        if (this.curetPage > this.totalPage - 1) {
          this.curetPage = 0
        }
        this.upDataCharts()
      }, 3000)
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.stock_Chart.offsetWidth / 100) * 3.6

      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
      }
      this.sellChart.setOption(adapterOption)
      // 手动的调用图表对象的resize 才能产生效果
      this.sellChart.resize()
    },
  },
}
</script>

<style></style>
